#include <stdio.h>
#include "ltv.h"


LTV ltv_decode(unsigned char *data) {
    LTV ltv = {0, 0, 0};
    // ?????
    ltv.length |= (*data) << 8;
    data++;
    ltv.length |= *data;
    data++;

    // ????Type
    ltv.type |= (*data) << 8;
    data++;
    ltv.type |= (*data);
    data++;

    // ????????
    ltv.value = data;

    return ltv;
}

void ltv_encode(unsigned char data_buff[], int type, const int *data) {

    int length = 6;
    // ???????
    data_buff[2] = (type & 0xff00) >> 8;
    data_buff[3] = (type & 0x00ff);

    // ???????
    while (*data != '\0') {
        data_buff[length] = *data;
        length++;
    }
    // ?????
    data_buff[0] = (length & 0xff00) >> 8;
    data_buff[1] = (length & 0x00ff);
}

void show_ltv(LTV ltv) {
    char *p = ltv.value;
    int length = ltv.length - 4;
    printf("LTV Show start!\n");
    printf("L:%d\tHEX:%X\n", ltv.length, ltv.length);
    printf("T:%d\tHEX:%X\n", ltv.type, ltv.type);
//    while (length>0){
//        printf("%X\t", *p);
//        p++;
//        length--;
//    }
    printf("\nLTV Show end!\n\n");
}